// custom page transition 
// some kind of like touch slider
var pageX, pageY, startX, startY, deltaX, deltaY, offsetY, offsetX, sliderWidth, sliderHeight, slider, slides, currentSlide, currentIndex, prevSlide, nextSlide, sweepDown, sweepUp, startTime, isScroll, isSubmiting;
slider = document.getElement('.slider');

var onTouchStart = function(e) {
	isScroll = false;
	slides = slider.getElements('.slide');
	currentSlide = slider.getElement('.active');
	currentIndex = slides.indexOf(currentSlide);
	prevSlide = currentIndex > 0 ? slides[currentIndex - 1] : null;
	nextSlide = currentIndex < slides.length - 1 ? slides[currentIndex + 1] : null;
	if(prevSlide){
		prevSlide.addClass('top');
		TweenMax.set(prevSlide, {y:"-100%"});
		prevSlide.getElements('[animated]').each(function(ele){
			
		});
	}
	if(nextSlide){
		nextSlide.addClass('bottom');
		TweenMax.set(nextSlide, {y: "100%"});
		nextSlide.getElements('[animated]').each(function(ele){
			
		});
	}
	startX = pageX = e.touches[0].pageX;
	startY = pageY = e.touches[0].pageY;
	deltaX = 0;
	deltaY = 0;
	startTime = Date.now();
	sweepDown = "";
};

var onTouchMove = function(e) {
	e.preventDefault();
	isScroll = true;
	deltaX = e.touches[0].pageX - pageX;
	deltaY = e.touches[0].pageY - pageY;
	pageX = e.touches[0].pageX;
	pageY = e.touches[0].pageY;
	//sweepDown = pageY - startY > 0 ? true : false;
	var opt = deltaY > 0 ? "+=" : "-=";
	if(prevSlide){
		TweenMax.set(prevSlide, {y: opt + Math.abs(deltaY) + "px"});
	}
	if(nextSlide){
		TweenMax.set(nextSlide, {y: opt + Math.abs(deltaY) + "px"});
	}

}

var onTouchEnd = function(e) { 
	deltaY = pageY - startY;
	var sn = Date.now() - startTime > 600 ? 200 : 50;

	if(Math.abs(deltaY) < sn){
		sweepDown = "nope";
	}else {
		if(deltaY > 0){
			sweepDown = "yes";
		}else {
			sweepDown = "no";
		}
	}
	if(sweepDown === "yes"){
		var focusEle = document.getElement('input[type=text]:focus');
		if(focusEle){
			focusEle.blur();
		}
		if(prevSlide){
			TweenMax.to(prevSlide, 0.4, {y: "0%", onComplete: function(){
				currentSlide.removeClass('active');
				prevSlide.removeClass('top').addClass('active');
			}});
			/*
			prevSlide.getElements('[animated]').each(function(ele){
				TweenMax.fromTo(ele, 2, {x:'-100%'}, {x:'0%'});
			});*/
			TweenMax.staggerFromTo(prevSlide.getElements('[animated]'), 2, {x:'-100%',opacity:0}, {x:'0%',opacity:1}, .5);
		}
		if(nextSlide){
			TweenMax.to(nextSlide, 0.4, {y: "100%", onComplete: function(){
				nextSlide.removeClass('bottom');
			}});
		}
	}else if(sweepDown === "no"){
		if(prevSlide){
			TweenMax.to(prevSlide, 0.4, {y: "-100%", onComplete: function(){
				prevSlide.removeClass('top');
			}});
		}
		if(nextSlide){
			TweenMax.to(nextSlide, 0.4, {y: "0%", onComplete: function(){
				currentSlide.removeClass('active');
				nextSlide.removeClass('bottom').addClass('active');
			}});
			/*
			nextSlide.getElements('[animated]').each(function(ele){
				TweenMax.fromTo(ele, 2, {x:'-100%'}, {x:'0%'});
			});
			*/
			TweenMax.staggerFromTo(nextSlide.getElements('[animated]'), 2, {x:'-100%',opacity:0}, {x:'0%',opacity:1}, .5);
		}
	}else{
		if(prevSlide){
			TweenMax.to(prevSlide, 0.4, {y: "-100%", onComplete: function(){
				prevSlide.removeClass('top');
			}});
		}
		if(nextSlide){
			TweenMax.to(nextSlide, 0.4, {y: "100%", onComplete: function(){
				nextSlide.removeClass('bottom');
			}});
		}
	}
}

if('ontouchstart' in window){
	window.addEvent('touchstart', onTouchStart);
	window.addEvent('touchmove', onTouchMove);
	window.addEvent('touchend', onTouchEnd);
	window.addEvent('touchcancel', onTouchEnd);
}
var form, ajaxWaitingMsg, submitHandle;

window.addEvent('domready', function(){
	form = $('op_activity');
	/*
	$('cover').addEvent('touchstart', function(){
		TweenMax.to(this, 0.4, {y:"100%"});
	});
	*/
	submitHandle =  function(e){
		//console.log(e);
		if(e && e.preventDefault)
			e.preventDefault();
		if(isSubmiting)
			return false;
		isSubmiting = true;
		//console.log('click submit button');
		var valid = true, focusEle, tossMsg;
		var data = {};
		$$('input[type=text],input[type=hidden],select')
			.each(function(field){
				var isRequired = field.hasClass('required');
				var is_mobile = field.hasClass('mobile');
				var value = field.get('value').trim();
				var name = field.get('name');
				data[name] = value;
				if(field.get('id') === "phone_id")
					return
				if(!value){
					if(!focusEle)
						focusEle = field;
					if(!tossMsg)
						tossMsg = '请把表单填写完整';
					valid = false;
				}else{
					if(is_mobile && !isMobile(value)){
						if(!focusEle)
							focusEle = field;
						if(!tossMsg)
							tossMsg = '请填入正确的手机号';
						valid = false;
					}
					if(field.get('id') == 'address' && field.hasClass('restrict')){
						if(!cityRE.test(value)){
							if(!focusEle)
								focusEle = field;
							if(!tossMsg)
								tossMsg = cityMessage;
							valid = false;	
						}
					}
				}
			});
		if(!valid){
			showMsg(tossMsg, 2500);
			focusEle.focus();
			isSubmiting = false;
			return false;
		}else{
			form.submit();
			
		}
	};
	
	
	if(navigator.userAgent.match(/Android/i)){
		$$('input[type=text]').each(function(ele){
			var wrap = ele.getParent('.wrap');
			ele.addEvent('focus', function(){
				wrap.addClass('editing');
			}).addEvent('blur', function(){
				wrap.removeClass('editing');
			});
		});
		
	}
	


	
});

